var express = require('express')
var router = express.Router()

// middleware that is specific to this router
router.use(function timeLog(req, res, next) {
    console.log('Time: ', Date.now())
    //模块路由中检测是否携带了 token
    if(!req.auth){
        return res.send({
            status: 403,
            message: "缺少token"
        });
    }
    next()
})

//  从db文件中导入的数据库模块
var mysql = require("../db/mysql")
// 审核列表
router.get('/list', (req, res) => {
    let {page,page_size,search} = req.query;
    // 查询limit
    let uid = req.auth.uid;// 用户id
    let start = (page - 1) * page_size;
    let limit = ` limit ${start}, ${page_size}` //limit 0,3
    // 搜索sql语句
    let searchSQL = `SELECT * FROM reapply WHERE emimf LIKE '%${search}%' ${limit}`
    mysql.query(searchSQL, function (error, results) {
        // res.json(results)————返回全部数据
        // 相应数据
        var date;
        var type;
        var imf;
        var position;
        var mobile;
        var id;
        let arr = [];
        for (let i = 0; i < results.length; i++) {
            date = results[i].subtime
            type = results[i].emtype
            imf = results[i].emimf
            position = results[i].position
            mobile = results[i].mobile
            id = results[i].id
            arr.push({ id, date, type, imf, position, mobile })
        }
        res.json({ code: 0, msg: "审核列表", data: arr[0] })
    })
    // res.json({ msg: "审核列表", code: 1, data: [] })
})
// 审核详情
router.post('/details', (req, res) => {
    // 先获取用户的请求数据，存到变量“body”中
    var body = req.body
    // 用结构复制把用户请求数据里面的的“mobile, pwd, utype”分别拿出来，方便接下来向数据库中添加数据时使用
    let { mobile, urole, emtype, position, emimf, extime, picture, state, subtime } = body
    // 添加数据
    mysql.query('INSERT INTO reapply (mobile,urole,emtype,position,emimf,extime,picture,state,subtime) VALUES (?,?,?,?,?,?,?,?,?)', [
        mobile,urole, emtype, position, emimf, extime, picture, state, subtime
    ], function (err, results) {
        // 响应数据
        if (err) {
            res.json({ code: 1,msg:"操作失败" })
        } else {
            res.json(
                {code:0, msg:"审核列表",
                data:{
                 mobile: mobile, emtype: emtype, position: position,
                emimf: emimf, extime: extime, picture: picture, state: state, subtime: subtime
            }})
        }
        // console.log(err + 'ok了!')
    });
    // res.json({ msg: "审核详情", code: 1, data: [] })
})

// 审核不通过说明
router.post('/illustrate', (req, res) => {
    var body = req.body
    let { emimf, emtype, position, noreason } = body
    mysql.query('INSERT INTO reapply (emimf,emtype,position,noreason) VALUES (?,?,?,?)',
        [emimf, emtype, position, noreason],
        function (err, results) {
            // 响应数据
            if (err) {
                res.json({ code: 1, msg:"操作失败",data: err  })
            } else {
                res.json({
                    code:0,msg:"审核不通过",
                    data:{emimf: emimf, emtype: emtype, position: position, reason: noreason}
                })
            }
        })
    // res.json({ msg: "审核不通过说明", code: 1, data: [] })
})
module.exports = router;